package com.weavernorth.performance;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.workflow.webservices.*;

public class AssistantServeFlow {
    private static Log log = LogFactory.getLog(AssistantServeFlow.class);
    //创建店员服务标准考核流程
    public void createRetail() {
        BaseBean bb = new BaseBean();
        RecordSet rsShop = new RecordSet();
        RecordSet rsShopTwo = new RecordSet();
        RecordSet rsGrade = new RecordSet();
        RecordSet rsClerk = new RecordSet();
        RecordSet rsClerkNum = new RecordSet();
        RecordSet rsPfr = new RecordSet();
        //查询店铺信息表获取数据
        String strShopsql = "select qyjl,fgs,dp,dc  from uf_dpxx  where ( sfdpphd=1  )  group by qyjl,fgs,dp,dc ";
        rsShop.executeQuery(strShopsql);
        while (rsShop.next()) {
            //获取店长姓名
            String strManager = Util.null2String(rsShop.getString("dc"));
            log.info("店长姓名：" + strManager);
            //获取店铺(名称)id
            String strDpid = Util.null2String(rsShop.getString("dp"));
            log.info("店铺(名称)id：" + strDpid);
            //获取分公司的值
            String strFgs = Util.null2String(rsShop.getString("fgs"));
            log.info("分公司的值：" + strFgs);
            //获取区域经理
            String strLsjl = Util.null2String(rsShop.getString("qyjl"));
            log.info("区域经理的值：" + strLsjl);
            //获取店铺信息其他数据的sql
            String strShopSqlTwo = "select ppkdx,dpzt,dpjb from uf_dpxx where  ( sfdpphd=1  )  and   dc='" + strManager + "' and dp='" + strDpid + "' and fgs='" + strFgs + "' and qyjl='" + strLsjl + "'";
            log.info("获取店铺信息其他数据的sql：" + strShopSqlTwo);
            rsShopTwo.executeQuery(strShopSqlTwo);
            String strBrand = "";
            String strStauts = "";
            String strRank = "";
            if (rsShopTwo.next()) {
                //品牌
                strBrand = Util.null2String(rsShopTwo.getString("ppkdx"));
                log.info("品牌：" + strBrand);
                //获取店铺状态
                strStauts = Util.null2String(rsShopTwo.getString("dpzt"));
                log.info("店铺状态：" + strStauts);
                //获取店铺级别
                strRank = Util.null2String(rsShopTwo.getString("dpjb"));
                log.info("店铺级别：" + strRank);
            }
            //创建流程的主表
            //存放主表字段的数组
            WorkflowRequestTableField[] wrti = new WorkflowRequestTableField[2];
            //添加区域经理字段
            log.info("区域经理：" + strLsjl);
            wrti[0] = new WorkflowRequestTableField();
            //字段在数据库中的字段名
            wrti[0].setFieldName("qyjl");
            //赋值
            wrti[0].setFieldValue(Util.null2String(strLsjl));
            wrti[0].setView(true);
            wrti[0].setEdit(true);
            wrti[0].setFieldOrder(0);
            wrti[0].setMand(false);
            //添加分公司字段
            log.info("分公司：" + strFgs);
            wrti[1] = new WorkflowRequestTableField();
            //字段在数据库中的字段名
            wrti[1].setFieldName("fgs");
            //赋值
            wrti[1].setFieldValue(Util.null2String(strFgs));
            wrti[1].setView(true);
            wrti[1].setEdit(true);
            wrti[1].setFieldOrder(0);
            wrti[1].setMand(false);


            WorkflowRequestTableRecord[] wrtri = new WorkflowRequestTableRecord[1];
            wrtri[0] = new WorkflowRequestTableRecord();
            wrtri[0].setWorkflowRequestTableFields(wrti);
            WorkflowMainTableInfo wmi = new WorkflowMainTableInfo();
            wmi.setRequestRecords(wrtri);


            //通过分公司值获取对应服务标准流程的workflowid的sql
            String strGradeSql = "select dykhlcfwbz from uf_khpflcpz where fgs='" + strFgs + "'";
            log.info("获取workflowid的sql：" + strGradeSql);
            rsGrade.executeQuery(strGradeSql);
            //判断是否有对应workflowid
            String strGradeWfId = "";
            if (rsGrade.next()) {
                strGradeWfId = Util.null2String(rsGrade.getString("dykhlcfwbz"));
                log.info("服务标准流程的workflowid：" + strGradeWfId);
            }
            //获取明细表数据的sql
            String strClerkSql = "select id , jobtitle,workcode from hrmresource where       departmentid='" + strDpid + "'  and  id!= '" + strManager + "' and (accounttype is null or accounttype='' or accounttype  ='0') and workcode is not null";
            log.info("获取明细数据的sql:" + strClerkSql);
            rsClerk.executeQuery(strClerkSql);
            //获取明细表长度的sql
            String strClerkNumSql = "select count(*) as num  from hrmresource where       departmentid='" + strDpid + "'  and  id!= '" + strManager + "' and (accounttype is null or accounttype='' or accounttype  ='0') and workcode is not null";
            log.info("获取明细表长度的sql：" + strClerkNumSql);
            rsClerkNum.executeQuery(strClerkNumSql);
            //明细表长度
            int intDetailNum = 0;
            if (rsClerkNum.next()) {
                intDetailNum = Integer.parseInt(Util.null2String(rsClerkNum.getString("num")));
                log.info("明细表的长度：" + intDetailNum);
            }
            //创建流程明细表
            //明细表个数为1
            WorkflowDetailTableInfo wdti[] = new WorkflowDetailTableInfo[1];
            //限定明细表的长度
            wrtri = new WorkflowRequestTableRecord[intDetailNum];
            //循环参数
            int intNum = 0;
            while (rsClerk.next()) {
                //获取店员姓名
                String strClerkName = Util.null2String(rsClerk.getString("id"));
                //获取店员工号
                String strClerkCode = Util.null2String(rsClerk.getString("workcode"));
                //获取店员岗位
                String strClerkJob = Util.null2String(rsClerk.getString("jobtitle"));

                //有8个字段长度为8
                wrti = new WorkflowRequestTableField[8];

                //添加店员姓名字段
                log.info("店员姓名：" + strClerkName);
                wrti[0] = new WorkflowRequestTableField();
                //字段在数据库中的字段名
                wrti[0].setFieldName("xm");
                //赋值
                wrti[0].setFieldValue(strClerkName);
                wrti[0].setView(true);
                wrti[0].setEdit(true);

                //添加店员工号字段
                log.info("店员工号：" + strClerkCode);
                wrti[1] = new WorkflowRequestTableField();
                //字段在数据库中的字段名
                wrti[1].setFieldName("gh");
                //赋值
                wrti[1].setFieldValue(strClerkCode);
                wrti[1].setView(true);
                wrti[1].setEdit(true);

                //添加店员岗位字段
                log.info("店员岗位：" + strClerkJob);
                wrti[2] = new WorkflowRequestTableField();
                //字段在数据库中的字段名
                wrti[2].setFieldName("zw");
                //赋值
                wrti[2].setFieldValue(strClerkJob);
                wrti[2].setView(true);
                wrti[2].setEdit(true);

                //添加店员分公司字段
                log.info("店员分公司：" + strFgs);
                wrti[3] = new WorkflowRequestTableField();
                //字段在数据库中的字段名
                wrti[3].setFieldName("fgs");
                //赋值
                wrti[3].setFieldValue(strFgs);
                wrti[3].setView(true);
                wrti[3].setEdit(true);

                //添加店员店铺名称字段
                log.info("店员店铺名称：" + strDpid);
                wrti[4] = new WorkflowRequestTableField();
                //字段在数据库中的字段名
                wrti[4].setFieldName("dpmc");
                //赋值
                wrti[4].setFieldValue(strDpid);
                wrti[4].setView(true);
                wrti[4].setEdit(true);

                //添加店铺级别字段
                log.info("店铺级别：" + strRank);
                wrti[5] = new WorkflowRequestTableField();
                //字段在数据库中的字段名
                wrti[5].setFieldName("dpjb");
                //赋值
                wrti[5].setFieldValue(strRank);
                wrti[5].setView(true);
                wrti[5].setEdit(true);

                //添加店铺状态字段
                log.info("店铺状态：" + strStauts);
                wrti[6] = new WorkflowRequestTableField();
                //字段在数据库中的字段名
                wrti[6].setFieldName("dpzt");
                //赋值
                wrti[6].setFieldValue(strStauts);
                wrti[6].setView(true);
                wrti[6].setEdit(true);

                //添加品牌字段
                log.info("品牌：" + strBrand);
                wrti[7] = new WorkflowRequestTableField();
                //字段在数据库中的字段名
                wrti[7].setFieldName("pp");
                //赋值
                wrti[7].setFieldValue(strBrand);
                wrti[7].setView(true);
                wrti[7].setEdit(true);


                wrtri[intNum] = new WorkflowRequestTableRecord();
                wrtri[intNum].setWorkflowRequestTableFields(wrti);
                intNum++;
            }
            wdti[0] = new WorkflowDetailTableInfo();
            wdti[0].setWorkflowRequestTableRecords(wrtri);


            //基本信息
            WorkflowBaseInfo wbi = new WorkflowBaseInfo();
            //流程的workflowid
            wbi.setWorkflowId(strGradeWfId);


            WorkflowRequestInfo wri = new WorkflowRequestInfo();
            //创建人根据级别不同划分不同区域经理
            String strCreator="1";
            //0和1对应级别A和A+
            if(!strRank.equals("0")&&!strRank.equals("1")){
                wri.setCreatorId(strLsjl);
                strCreator=strLsjl;
            }
            else{
                //查询评分人的sql
                String strNameSql="select  pfr from uf_khpflcpfrpz  where dppp='"+strBrand+"' and dpjb='"+strRank+"'  and  pflx='1'";
                rsPfr.executeQuery(strNameSql);
                if(rsPfr.next()){
                    String strPfr=Util.null2String(rsPfr.getString("pfr"));
                    strCreator=strPfr;
                    log.info("A或A+级别的店铺对应评分人："+strPfr);
                    wri.setCreatorId(strPfr);
                }
            }
            //紧急程度
            wri.setRequestLevel("0");
            //请求的名称
            wri.setRequestName("店员服务标准考核流程");
            //停留在提交节点
            wri.setIsnextflow("0");

            //主表数据
            wri.setWorkflowMainTableInfo(wmi);
            wri.setWorkflowBaseInfo(wbi);


            //明细表数据
            wri.setWorkflowDetailTableInfos(wdti);
            WorkflowServiceImpl wfsi = new WorkflowServiceImpl();

            String strRequestid = wfsi.doCreateWorkflowRequest(wri, Integer.parseInt(strCreator));
            log.info("生成流程的requestid:" + strRequestid);


        }


    }
}